<template>
    <div class="user-content">
        <el-row :gutter="20" style="margin: 10px;">
            <el-col :md="24">
                <page-main style="margin: 10px 0;">
                    <div class="flc">
                        <h3 style="margin-right: auto;">用户筛选</h3>
                        <el-button type="primary" icon="el-icon-search" class="my-btn" @click="_getMemberList()">搜索</el-button>
                    </div>
                    <el-form ref="search" :model="search" label-width="80px" size="small">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="用户名">
                                    <el-input v-model="search.nickname" placeholder="请输入用户名" clearable />
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="手机号">
                                    <el-input v-model="search.mobile" placeholder="请输入手机号" clearable />
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="加入时间">
                                    <el-date-picker
                                        v-model="search.joinTime"
                                        size="small"
                                        type="datetimerange"
                                        range-separator="至"
                                        start-placeholder="开始日期"
                                        end-placeholder="结束日期"
                                        value-format="yyyy-MM-dd HH:mm:ss"
                                    />
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="最后登录">
                                    <el-date-picker
                                        v-model="search.LoginTime"
                                        size="small"
                                        type="datetimerange"
                                        range-separator="至"
                                        start-placeholder="开始日期"
                                        end-placeholder="结束日期"
                                        value-format="yyyy-MM-dd HH:mm:ss"
                                    />
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="选择来源">
                                    <el-radio-group v-model="search.source">
                                        <el-radio-button :label="6">全部</el-radio-button>
                                        <el-radio-button :label="1">苹果APP</el-radio-button>
                                        <el-radio-button :label="2">安卓APP</el-radio-button>
                                        <el-radio-button :label="3">公众号</el-radio-button>
                                        <el-radio-button :label="4">小程序</el-radio-button>
                                    </el-radio-group>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="会员等级">
                                    <el-radio-group v-model="search.level">
                                        <el-radio-button label="1">全部</el-radio-button>
                                        <el-radio-button label="2">VIP</el-radio-button>
                                    </el-radio-group>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="用户标签">
                                    <el-checkbox-group v-model="search.tag_ids" @change="changeTag">
                                        <el-checkbox v-for="tag in tagList" :key="tag.id" :label="tag.id">{{ tag.name }}</el-checkbox>
                                        <!--<el-checkbox label="1">婚姻家事</el-checkbox>
                                        <el-checkbox label="2">房产纠纷</el-checkbox>
                                        <el-checkbox label="3">公司工商</el-checkbox>
                                        <el-checkbox label="4">交通事故</el-checkbox>
                                        <el-checkbox label="5">合同纠纷</el-checkbox>
                                        <el-checkbox label="6">债权纠纷</el-checkbox>
                                        <el-checkbox label="7">劳动用工</el-checkbox>
                                        <el-checkbox label="8">刑事辩护</el-checkbox>
                                        <el-checkbox label="9">行政复议</el-checkbox>
                                        <el-checkbox label="10">行政诉讼</el-checkbox>-->
                                    </el-checkbox-group>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div class="flc">
                        <h3 style="margin-right: auto;">用户列表</h3>
                        <el-button type="primary" class="my-btn">导出信息</el-button>
                    </div>
                    <el-table
                        :data="tableData"
                        :header-cell-style="{background:'#f2f2f2',color:'#555',textAlign: 'center'}"
                        border
                        style="width: 100%;"
                    >
                        <el-table-column label="用户名/地区" align="left" width="240">
                            <template slot-scope="scope">
                                <div class="over-hidden">
                                    <img class="avatar" :src="scope.row.avatar || require('../../assets/images/logo.png')" alt="">
                                    <div class="info">
                                        <div class="top">
                                            <svg-icon v-if="scope.row.source == 1" name="ios" />
                                            <svg-icon v-if="scope.row.source == 2" name="android" />
                                            <svg-icon v-if="scope.row.source == 0" name="question" />
                                            <svg-icon v-if="scope.row.source == 4" name="mini-program" />
                                            <svg-icon v-if="scope.row.source == 3" name="weixin" />
                                            <span style="padding-left: 5px;">{{ scope.row.nickname || '无名' }}</span>
                                            <el-tag v-if="scope.row.ismember === 1" size="mini" effect="dark" color="#5482ee" style="border-color: transparent; margin-left: 5px;">VIP</el-tag>
                                        </div>
                                        <div class="bot">{{ scope.row.province }}-{{ scope.row.city }}</div>
                                    </div>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column label="用户资产" align="center">
                            <template slot-scope="scope">
                                <div>
                                    <svg-icon name="rmb" />{{ scope.row.balance || 0 }}
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="mobile" label="手机号" align="center" />
                        <el-table-column label="加入时间/最后登录" align="center">
                            <template slot-scope="scope">
                                <div>{{ scope.row.create_time }}</div>
                                <div>{{ scope.row.last_time }}</div>
                            </template>
                        </el-table-column>
                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button type="text" size="small" @click="handleClick(scope.row, 0)">编辑</el-button>
                                <el-button type="text" size="small" @click="handleClick(scope.row, 1)">订单</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <Pagination
                        :current-page="currentPage"
                        :page-sizes="pageSizes"
                        :page-size="pageSize"
                        :total="total"
                        @handle-size-change="handleSizeChange"
                        @handle-current-change="handleCurrentChange"
                    />
                </page-main>
            </el-col>
        </el-row>
        <el-dialog
            class="personnel-dialog"
            title="用户编辑"
            :visible.sync="addLawyerDialog"
            width="60%"
            :show-close="false"
            :close-on-click-modal="false"
        >
            <el-form ref="addLawyerForm" :model="addLawyerForm" :rules="rules" label-width="80px" size="small">
                <el-row :gutter="10" type="flex" justify="space-between">
                    <el-col :span="11">
                        <el-form-item label="用户名" prop="nickname">
                            <el-input v-model="addLawyerForm.nickname" placeholder="请输入用户名" />
                        </el-form-item>
                        <el-form-item label="选择地区">
                            <el-cascader
                                v-model="addLawyerForm.selectedOptions"
                                style="width: 100%;"
                                :options="cityTwo"
                                :separator="' '"
                                clearable
                                @change="handleChangeCity"
                            />
                        </el-form-item>
                        <el-form-item label="联系电话" prop="mobile">
                            <el-input v-model="addLawyerForm.mobile" clearable />
                        </el-form-item>
                        <el-form-item label="性别" prop="sex">
                            <el-radio v-model="addLawyerForm.sex" label="1">男</el-radio>
                            <el-radio v-model="addLawyerForm.sex" label="2">女</el-radio>
                            <!--<el-radio v-model="addLawyerForm.sex" label="3">未知</el-radio>-->
                        </el-form-item>
                        <el-form-item label="邮箱" prop="email">
                            <el-input v-model="addLawyerForm.email" placeholder="请输入邮箱" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="11">
                        <el-form-item label="用户头像">
                            <div class="upload" style="position: relative;">
                                <el-upload
                                    class="avatar-uploader my-upload"
                                    action=""
                                    :show-file-list="false"
                                    :before-upload="beforeAvatarUpload"
                                    :http-request="uploadFile"
                                    :on-remove="handleRemove"
                                >
                                    <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                    <i v-else class="el-icon-plus avatar-uploader-icon" />
                                </el-upload>
                                <i v-show="imageUrl" class="el-icon-close upload-del" style="cursor: pointer;" @click="deleteAvatar" />
                                <img class="img" src="../../assets/images/logo.png" alt="">
                            </div>
                            <el-progress v-show="isShowPercentage" :percentage="percentage" />
                            <div style="font-size: 12px;">图片建议尺寸300*300px</div>
                        </el-form-item>
                        <el-form-item label="备注" prop="introduction">
                            <el-input v-model="addLawyerForm.introduction" type="textarea" maxlength="200" placeholder="请输入内容" show-word-limit />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancelLawyerDialog">取 消</el-button>
                <el-button type="primary" @click="submit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import api from '@/api'
import cityTwo from '@/assets/country-data-master/country-level2-data'
import { getMemberList, getMemberFind, memberEdit } from '@/api/userApi'
import { getTagList } from '@/api/lawyerApi'
export default {
    name: 'UserPage',
    data() {
        return {
            search: {
                nickname: '',
                mobile: '',
                joinTime: [],
                endTime: [],
                source: 6,
                level: '1',
                tag_ids: []
            },
            tagList: [],
            currentPage: 1,
            pageSize: 10,
            total: 0,
            pageSizes: [5, 10, 15, 20],
            tableData: [
                {
                    date: '2016-05-03',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1518 弄'
                },
                {
                    date: '2016-05-02',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1518 弄'
                },
                {
                    date: '2016-05-04',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1518 弄'
                }
            ],
            addLawyerForm: {
                name: '',
                val: '',
                value: '',
                selectedOptions: [],
                introduce: '',
                radio: '1',
                email: ''
            },
            addLawyerDialog: false,
            rules: {
                nickname: [
                    {required: true, message: '请输入用户名', trigger: 'blur'}
                ],
                mobile: [
                    {required: true, message: '请输入联系电话', trigger: 'blur'}
                ],
                sex: [
                    {required: false, message: '请选择性别', trigger: 'change'}
                ],
                email: [
                    {required: false, message: '请输入邮箱', trigger: 'blur'}
                ],
                introduction: [
                    {required: false, message: '请输入备注', trigger: 'blur'}
                ]
            },
            options: [
                {
                    value: '选项1',
                    label: '黄金糕'
                },
                {
                    value: '选项2',
                    label: '双皮奶'
                }
            ],
            cityTwo: cityTwo,
            imageUrl: '',
            dialogVisibleUpImg: false,
            percentage: 0,
            isShowPercentage: false,
            editId: ''
        }
    },
    created() {
        this.$SpinkitLoading({type: 'chase', size: 50, color: '#ffffff'})
        this._getMemberList()
        this._getTagList()
    },
    methods: {
        async _getTagList() {
            const res = await getTagList()
            console.log(res)
            this.tagList = res.data.list.reverse()
        },
        async _getMemberList() {
            let data = {
                p: this.currentPage,
                length: this.pageSize,
                nickname: this.search.nickname,
                mobile: this.search.mobile,
                create_time_start: this.search.joinTime[0] ? this.search.joinTime[0] : '',
                create_time_end: this.search.joinTime[1] ? this.search.joinTime[1] : '',
                last_time_start: this.search.endTime[0] ? this.search.endTime[0] : '',
                last_time_end: this.search.endTime[1] ? this.search.endTime[1] : '',
                tag_ids: this.search.tag_ids,
                source: this.search.source === 6 ? '' : this.search.source
            }
            const res = await getMemberList(data)
            this.tableData = res.data.list
            this.total = res.data.total
            this.$SpinkitLoading.close()
            console.log(res)
        },
        changeTag(val) {
            console.log(val)
        },
        // 分页 2 个方法
        handleSizeChange(val) {
            console.log(`每页 ${val} 条`)
            this.pageSize = val
            this.$SpinkitLoading({type: 'chase', size: 50, color: '#ffffff'})
            this._getMemberList()
        },
        handleCurrentChange(val) {
            console.log(`当前页: ${val}`)
            this.currentPage = val
            this.$SpinkitLoading({type: 'chase', size: 50, color: '#ffffff'})
            this._getMemberList()
        },
        handleClick(row, index) {
            console.log(row, index)
            if (index === 0) {
                this.addLawyerDialog = true
                this._getMemberFind(row.id)
                this.editId = row.id
            }
        },
        async _getMemberFind(id) {
            const res = await getMemberFind({id: id})
            this.addLawyerForm = res.data.list
            this.addLawyerForm.sex = res.data.list.sex + ''
            this.addLawyerForm.selectedOptions = [res.data.list.province, res.data.list.city]
            this.imageUrl = res.data.list.avatar
            console.log(res)
        },
        deleteAvatar() {
            this.imageUrl = ''
        },
        handleChangeCity(value) {
            console.log(value)
        },
        beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
            const isLt2M = file.size / 1024 / 1024 < 2
            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG 或者 PNG 格式!')
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!')
            }
            return isJPG && isLt2M
        },
        uploadFile(data) {
            console.log(data)
            this.isShowPercentage = true
            const _file = data.file
            let formData = new FormData()
            formData.append('image', _file)
            // const res = await uploadImage(formData)
            // console.log(res)
            // this.imageUrl = res.data.filename
            api.post('/client/visitor/uploadImage', formData, {
                onUploadProgress: progressEvent => {
                    console.log(progressEvent)
                    this.percentage = progressEvent.loaded / progressEvent.total * 100 | 0
                }
            }).then(res => {
                console.log(res)
                this.isShowPercentage = false
                this.imageUrl = res.data.filename
                this.$message.success('上传成功')
            }).catch(err => {
                console.log(err)
                this.$message.error('网络超时')
            })
        },
        handleRemove(file) {
            console.log(file)
        },
        cancelLawyerDialog() {
            this.addLawyerDialog = false
            this.resetForm('addLawyerForm')
        },
        submit() {
            this.$refs.addLawyerForm.validate(async valid => {
                if (!valid) return false
                console.log(this.addLawyerForm)
                let data = {
                    id: this.editId,
                    nickname: this.addLawyerForm.nickname,
                    mobile: this.addLawyerForm.mobile,
                    sex: this.addLawyerForm.sex ? this.addLawyerForm.sex - 0 : 0,
                    email: this.addLawyerForm.email,
                    province: this.addLawyerForm.selectedOptions[0],
                    city: this.addLawyerForm.selectedOptions[1],
                    avatar: this.imageUrl || '',
                    remark: this.addLawyerForm.introduction
                }
                console.log(data)
                const res = await memberEdit(data)
                console.log(res)
                this.$message.success('修改成功')
                this.addLawyerDialog = false
                this._getMemberList()
            })
        },
        resetForm(formName) {
            this.$refs[formName].resetFields()
        }
    }
}
</script>

<style scoped lang="scss">
    .over-hidden {
        display: flex;
        align-items: center;
        .avatar {
            width: 38px;
            height: 38px;
            border-radius: 50%;
            margin-right: 10px;
        }
        .info {
            .top {
                display: flex;
                align-items: center;
            }
            .bot {
                font-size: 12px;
                color: #909399;
            }
        }
    }
    .upload {
        display: flex;
        .img {
            width: 120px;
            height: 120px;
            margin-left: 15px;
            border-radius: 6px;
        }
    }
    .upload-del {
        position: absolute;
        left: 120px;
        top: -10px;
    }
</style>
